/**
 * @description: 格式化枚举
 * @param {any} o
 * @param {string} s
 * @return {*}
 */
export const formatObjectModelEnumOptions = (o: any, s: string): Array<{ label: string; value: string }> => {
  const t = o[s];
  return Object.keys(t).map(key => ({ value: key, label: t[key] }));
};

/**
 * @description:  计算数组种的最大值和最小值
 * @param {Array} arr
 * @param {number} defaultValue
 * @param {*} type  "min" | "max"
 * @return {Record<"min" | "max", number | null> | number | null}
 */
export const calcArrayExtremum = (
  arr: Array<number | any> | any,
  defaultValue?: number | undefined | null,
  type?: "min" | "max"
): Record<"min" | "max", number | null> | number | null => {
  if (!Array.isArray(arr) || !arr?.length) return defaultValue || null;
  const { min = null, max = null } = arr.reduce(
    (pre: any, cur: any) => {
      if (typeof cur === "number" && !Object.is(cur, NaN)) {
        const { min = null, max = null } = pre;
        if (min === null) {
          pre.min = cur;
        } else if (cur < min) {
          pre.min = cur;
        }
        if (max === null) {
          pre.max = cur;
        } else if (cur > max) {
          pre.max = cur;
        }
      }
      return pre;
    },
    { min: null, max: null }
  );
  if (min === null && max === null) return defaultValue || null;
  switch (type) {
    case "min":
      return min;
    case "max":
      return max;
    default:
      return { min, max };
  }
};

// 将base64图片地址改成blob格式
export const dataURLtoBlob = url => {
  let arr = url?.split(","),
    mime = arr[0]?.match(/:(.*?);/)[1],
    bstr = window.atob(arr[1]),
    n = bstr.length,
    u8arr = new Uint8Array(n);
  while (n--) {
    u8arr[n] = bstr.charCodeAt(n);
  }
  return new Blob([u8arr], { type: mime });
};

/**
 * @description: 格式化缺失数据
 * @param {Array<number>} data
 * @return {*}
 */
export const formatMissingData = (data: Array<number | any>) => {
  if (!data?.length) return [];
  let value = data.find(item => typeof item === "number" || typeof item === "string");
  value = Number(value);
  return data.map(item => {
    if (typeof item === "number") {
      value = item;
    } else if (typeof item === "string") {
      if (item) {
        item = Number(item);
        value = item;
      } else {
        item = value;
      }
    } else {
      item = value;
    }
    return item;
  });
};

/**
 * @description: 格式化账号名称
 * @param {string} account  账号
 * @param {string} name  名称
 * @return {*}
 */
export const formatAccountName = (account?: string, name?: string) => {
  const ar = [account, name]?.filter(item => !!item);
  return ar?.join("-");
};
/**
 * @description: 去除字符串类型参数的前后空格
 * @param {object} params  参数集合
 */
export const stringTrimOfParams = params => {
  // 判断参数是否是对象类型，否则直接返回
  if (Object.prototype.toString.call(params) === "[object Object]") {
    const obj = {};
    Object.keys(params).forEach(key => {
      const val = params[key];
      // 判断参数是否为字符串类型，是则去除前后空格，否则直接返回
      if (typeof val === "string") {
        obj[key] = val.trim();
      } else {
        obj[key] = val;
      }
    });
    return obj;
  }
  return params;
};

/**
 * 原生通用导出图片
 * @param canvasDom
 * @param name
 * @param type
 * @param encoderOptions
 * @param backgroundColor
 */
export function exportCanvasImg(canvasDom: any, name = "导出图片", type = "image/png", encoderOptions = 1, backgroundColor = "white") {
  const ctx = canvasDom.getContext("2d");
  // 保存当前状态
  ctx.save();
  // 设置复合操作
  ctx.globalCompositeOperation = "destination-over";
  // 绘制白色背景
  ctx.fillStyle = backgroundColor;
  ctx.fillRect(0, 0, canvasDom.width, canvasDom.height);
  // 恢复状态
  ctx.restore();
  // 3. 绘制原始内容
  const dataURL = canvasDom.toDataURL(type, encoderOptions);
  const link = document.createElement("a");
  link.download = name;
  link.href = dataURL;
  link.click();
  link.remove();
}
